package vip.zhenzicheng.concurrent_program.thread;

import java.util.concurrent.*;

/**
 * 异步任务 + 线程池
 *
 * @author zhenzicheng
 * @date 2022-06-03 17:53
 */
public class FutureTaskThreadPoolDemo {

  public static void main(String[] args) throws ExecutionException, InterruptedException {

    ExecutorService threadPool = Executors.newFixedThreadPool(3);

    long start = System.currentTimeMillis();
    FutureTask<String> task1 = new FutureTask<>(() -> {
      TimeUnit.MILLISECONDS.sleep(500);
      return "task1 over";
    });
    FutureTask<String> task2 = new FutureTask<>(() -> {
      TimeUnit.MILLISECONDS.sleep(400);
      return "task2 over";
    });
    FutureTask<String> task3 = new FutureTask<>(() -> {
      TimeUnit.MILLISECONDS.sleep(300);
      return "task3 over";
    });
    threadPool.submit(task1);
    threadPool.submit(task2);
    threadPool.submit(task3);

    // task1.get();
    // task2.get();
    // task3.get();

    long end = System.currentTimeMillis();
    System.out.println("cost:" + (end - start) / 1000.0);

    threadPool.shutdown();
  }
}
